package com.cc.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cc.blog.entity.Comment;
import com.cc.blog.entity.SysUser;
import com.cc.blog.mapper.CommentMapper;
import com.cc.blog.service.CommentsService;
import com.cc.blog.service.SysUserService;
import com.cc.blog.utils.UserThreadLocal;
import com.cc.blog.vo.params.CommentParam;
import com.cc.blog.vo.CommentVo;
import com.cc.blog.vo.Result;
import com.cc.blog.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentsServiceImpl implements CommentsService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private SysUserService sysUserService;


    @Override
    public Result commentsByArticleId(Long id) {
        /**
         * 1.根据文章id查询评论列表，从comment表中查询
         * 2.根据作者的id查询作者的信息
         * 3.判断level 如果 = 1 要去查询它有没有子评论
         * 4.如果有，根据评论id去查询parent_id
         */
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getArticleId,id).eq(Comment::getLevel,1);
        List<Comment> comments = commentMapper.selectList(queryWrapper);
        List<CommentVo> commentVoList = copyList(comments);
        return Result.success(commentVoList);
    }

    @Override
    public Result writeComment(CommentParam commentParam) {
        /**
         * 1.从threadLocal中获取用户信息
         * 2.判断有没有parent,设定level是级别
         * 3.从参数对象中找数据并赋值给用户id
         * 4.将评论对象插入数据库
         */
        SysUser sysUser = UserThreadLocal.get();
        Comment comment = new Comment();
        //设置文章id,作者id，内容，日期
        comment.setAuthorId(sysUser.getId());
        comment.setArticleId(commentParam.getArticleId());
        comment.setContent(commentParam.getContent());
        comment.setCreateDate(System.currentTimeMillis());
        //判断parent定义等级
        if (commentParam.getParent() == null || commentParam.getParent() == 0){
            comment.setLevel(1);
            comment.setParentId(0L);
        }else{
            comment.setLevel(2);
            comment.setParentId(commentParam.getParent());
        }
        //因为是Long型,要把null转为0?
        comment.setToUid(commentParam.getToUserId() == null ? 0 : commentParam.getToUserId());
        commentMapper.insert(comment);
        return Result.success(null);
    }

    private List<CommentVo> copyList(List<Comment> comments) {
        List<CommentVo> commentVoList = new ArrayList<>();
        for (Comment comment : comments) {
            CommentVo commentVo = copy(comment);
            commentVoList.add(commentVo);
        }
        return commentVoList;
    }

    private CommentVo copy(Comment comment) {
        CommentVo commentVo = new CommentVo();
        BeanUtils.copyProperties(comment,commentVo);
        //获取作者信息
        Long authorId = comment.getAuthorId();
        UserVo userVoById = sysUserService.findUserVoById(authorId);
        commentVo.setAuthor(userVoById);
        //子评论
        Integer level = comment.getLevel();
        if (1 == level){
            Long articleId = comment.getArticleId();
            List<CommentVo> commentVoList =  findCommentsByParentId(articleId);
            commentVo.setChildren(commentVoList);
        }
        //给谁评论
        if (level > 1){
            Long toUid = comment.getToUid();
            UserVo toUserVo = sysUserService.findUserVoById(toUid);
            commentVo.setToUser(toUserVo);
        }
        return commentVo;
    }

    private List<CommentVo> findCommentsByParentId(Long articleId) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getParentId,articleId).eq(Comment::getLevel,2);
        return copyList(commentMapper.selectList(queryWrapper));
    }
}
